<template>
  <div class="personal">
    <router-link :to="'/edit_profile/' + currentuser.id">
      <div class="profile">
        <img :src="currentuser.head_img" alt />
        <div class="profile-center">
          <div class="name">
            <span class="iconfont iconxingbienan"></span
            >{{ currentuser.nickname }}
          </div>
          <div class="time">
            {{ currentuser.create_date | dateFormat("/") }}
          </div>
        </div>
        <span class="iconfont iconjiantou1"></span>
      </div>
    </router-link>
    <mycell
      title="我的关注"
      desc="关注的用户"
      @click="$router.push({ name: 'user_follows' })"
    >
    </mycell>
    <mycell title="我的跟帖" desc="跟帖/回复"> </mycell>
    <mycell
      title="我的收藏"
      desc="文章/视频"
      @click="$router.push({ name: 'user_star' })"
    >
    </mycell>
    <mycell title="设置" desc=""> </mycell>
    <mybutton type="primary" class="btn" @click="exitApp">退出</mybutton>
  </div>
</template>

<script>
// 引入所需组件
import mycell from "@/components/mycell.vue";
import mybutton from "@/components/mybutton.vue";
import axios from "@/utils/myaxios";
import { dateFormat } from "@/utils/myfilter.js";

import { getUserInfo } from "@/api/user.js";

export default {
  data() {
    return {
      currentuser: {},
    };
  },
  components: {
    mycell,
    mybutton,
  },
  methods: {
    exitApp() {
      localStorage.removeItem("hm_toutiao_56");
      this.$router.push({ path: "/index" });
    },
  },
  // 由于我们封装是函数，所以得进行注册，确定它是用于过滤器
  filters: {
    dateFormat,
  },
  async mounted() {
    // console.log(this.$route);
    let id = this.$route.params.id;
    // console.log(id);
    let res = await getUserInfo(id);
    // console.log(res);
    // 细节注意：图片不一定有
    // 新用户第一次进入页面，图片是不存在的。如果存在也要注意只有目录+文件名
    if (res.data.message === "获取成功") {
      this.currentuser = res.data.data;
      if (this.currentuser.head_img) {
        this.currentuser.head_img =
          axios.defaults.baseURL + this.currentuser.head_img;
      } else {
        this.currentuser.head_img =
          axios.defaults.baseURL + "/uploads/image/default.png";
      }
    } else {
      this.$router.push({ name: "Login" });
    }
  },
};
</script>
<style lang="less" scoped>
.personal {
  width: 100vw;
  height: 100vh;
  background-color: #eee;
}
a {
  color: #666;
}
.profile {
  display: flex;
  padding: 20px 10px;
  justify-content: space-between;
  align-items: center;
  border-bottom: 5px #ddd solid;

  img {
    width: 70 / 360 * 100vw;
    height: 70 / 360 * 100vw;
    border-radius: 50%;
  }

  .profile-center {
    flex: 1;
    padding: 0 10px;
  }

  .name {
    span {
      color: #75b9eb;
    }
  }

  .time {
    color: #666;
    font-size: 14px;
    margin-top: 5px;
  }
}
</style>